package com.wyp168.servicebase.exceptionhandler;

import com.wyp168.commonutils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

//@ControllerAdvice
//@ResponseBody
//下面这个注解封装了了上面那两个注解
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    /**
     * 全局异常
     */
    @ExceptionHandler(Exception.class)
    public R error(Exception e) {
        e.printStackTrace();
        return R.error().message("执行了全局的异常处理方法");
    }

    /**
     * 特定异常
     */
    @ExceptionHandler(ArithmeticException.class)
    public R error(ArithmeticException e) {
        e.printStackTrace();
        return R.error().message("执行了ArithmeticException处理方法");
    }

    /**
     * 自定义异常
     */
    @ExceptionHandler
    public R error(GuliException e) {
        e.printStackTrace();
        log.error(e.getMsg());
        return R.error().message(e.getMsg());
    }

}
